<?php

// +----------------------------------------------------------------------
// |ZBPHP[基于ThinkPHP5.1开发]
// +----------------------------------------------------------------------
// | Copyright (c) 2016-2019 http://www.benbenwangluo.com
// +----------------------------------------------------------------------
// | Author 似水星辰 [ 2630481389@qq.com ]
// +----------------------------------------------------------------------
// | 中犇软件 技术六部 出品
// +----------------------------------------------------------------------

namespace app\order\admin;

use app\admin\admin\Base;
use service\Format;


/**
 * 订单提醒
 * @author 晓风<215628355@qq.com>
 * @date 2021-11-6 17:30:59
 */
class Remind extends Base {

    
    /**
     * 订单提醒
     * @return mixed
     */
    public function remind()
    {
        cookie('__forward__', $_SERVER['REQUEST_URI']);
        // 查询
        $map[] = [];
        // 排序
        $order = 'id desc';
        // 数据列表
        $data_list = \think\Db::name('order_remind')->alias('r')
            ->join('order o', 'r.order_sn=o.order_sn', 'left')
            ->field('r.id,r.order_sn,r.create_time,r.status,o.create_time as order_time,o.user_id,o.order_type')
            ->order($order)
            ->paginate()
            ->each(function ($item) {
                switch ($item['order_type']) {
                    case 3:
                        $action = 'order/index';
                        break;
                    case 5:
                        $action = 'activity/group';
                        break;
                    case 6:
                        $action = 'activity/seckill';
                        break;
                    case 7:
                        $action = 'activity/presell';
                        break;
                    case 8:
                        $action = 'activity/presell';
                        break;
                    case 9:
                        $action = 'activity/discount';
                        break;
                    case 10:
                        $action = 'activity/cut';
                        break;
                    case 11:
                        $action = 'order/index';
                        break;
                    default:
                        $action = 'order/index';
                        break;
                }
                $item['order_sn_link'] = "<a href='" . url('order/' . $action, ['order_sn' => $item['order_sn']]) . "'>" . $item['order_sn'] . "</a>";
                $item['create_time'] = date('Y-m-d H:i:s', $item['create_time']);
                $item['order_time'] = date('Y-m-d H:i:s', $item['order_time']);
                return $item;
            });;

        $buttons = [
            [
                'ident' => 'detail',//按钮标识
                'title' => lang('查看详情'), //标题
                'href' => ['order/order/detail', ['order_sn' => '__order_sn__']],//链接
                'icon' => 'fa fa-eye pr5',//图标
                'class' => 'btn btn-xs mr5 btn-default btn-flat'//样式类
            ], [
                'ident' => 'read',//按钮标识
                'title' => lang('标记已读'), //标题
                'href' => ['remind_read', ['rid' => '__id__']],//链接
                'icon' => 'fa fa-eye pr5',//图标
                'class' => 'btn btn-xs mr5 btn-default btn-flat ajax-get'//样式类
            ], [
                'ident' => 'del',//按钮标识
                'title' => lang('删除'), //标题
                'href' => ['remind_del', ['rid' => '__id__']],//链接
                'icon' => 'fa fa-close pr5',//图标
                'class' => 'btn btn-xs mr5 btn-default btn-flat  ajax-get'//样式类
            ]
        ];
        $fields = [
            ['id', 'ID'],
            ['order_sn_link', lang('订单编号')],
            ['create_time', lang('提醒时间')],
            ['order_time', lang('下单时间')],
            ['user_id', lang('下单人'), 'callback', 'get_nickname'],
            ['status', lang('状态'), 'status', '', [lang('未读'), lang('已读')]],
            ['right_button', lang('操作'), 'btn']
        ];
        return Format::ins()
            ->addColumns($fields)
            ->setRightButtons($buttons)
            ->replaceRightButton(['status' => 1], '', 'read')
            ->hideCheckbox()
            ->setData($data_list)
            ->fetch();
    }

    /**
     * 提醒已读
     */
    public function remind_read($rid)
    {
        $rs = \think\Db::name('order_remind')->where('id', $rid)->setField('status', 1);
        if ($rs) {
            $this->success(lang('已读成功'));
        } else {
            $this->error(lang('修改失败'));
        }
    }

    /**
     * 提醒删除
     * @param $rid
     */
    public function remind_del($rid)
    {
        $rs = \think\Db::name('order_remind')->delete($rid);
        if ($rs) {
            $this->success(lang('删除成功'));
        } else {
            $this->error(lang('删除失败'));
        }
    }
    
    
    public function remind_update()
    {
        \think\facade\Log::close();//临时关闭日志写入
        $num = \think\Db::name('order_remind')->field('id,order_sn')->where('status', 0)->order('id desc')->count();
        $remind = \think\Db::name('order_remind')->field('id,order_sn')->where('status', 0)->order('id desc')->limit(5)->select();
        foreach ($remind as $k => $v) {
            $remind[$k]['href'] = url('order/order/detail', ['order_sn' => $v['order_sn']]);
        }
        echo json_encode([
            'num' => $num,
            'listData' => $remind
        ]);
    }

}
